<?php
/**
 * Core_Role_EditForm
 * 
 * @author naspeh
 * @version $Id: EditForm.php 671 2008-06-30 12:06:16Z naspeh $
 */
class Core_Role_EditForm extends Core_Form 
{
    /**
     * Инициализация
     *
     * @return Core_Role_EditForm
     */
    public function init()
    {
        $roleEdit = $this->getParams('role');
        $this->addSubForm(Core_Table_Roles::getFormParents($roleEdit), 'parents');
        $acl = Core_Acl::getRegisrty();
        $roleEdit = new Core_Acl_Resource_Role($roleEdit);
        $acl->active($roleEdit);
        if (false !== ($privilege = $acl->getAllowPrivileges(Core_Auth::get('id'), $roleEdit->getResourceId(), 'is_login')))
        {
            $this->addElement('checkbox', 'is_login', array(
                'label' => $privilege['desc'],
                'checked' => $roleEdit->is_login,
                'decorators' => array(
                    'ViewHelper',
                    array('Label', array('placement'=>'APPEND')),
                    array('HtmlTag', array('tag' => 'div'))
                )
            ));           
        }
        if (false !== ($privilege = $acl->getAllowPrivileges(Core_Auth::get('id'), $roleEdit->getResourceId(), 'is_system')))
        {
            $this->addElement('checkbox', 'is_system', array(
                'label' => $privilege['desc'],
                'checked' => $roleEdit->is_system,
                'decorators' => array(
                    'ViewHelper',
                    array('Label', array('placement'=>'APPEND')),
                    array('HtmlTag', array('tag' => 'div'))
                )
            ));           
        }
        $this->addElement('button', 'submit', array(
            'type'     => 'submit',
            'label'    => 'Сохранить',
            'attribs'  => array(
                'class' => 'positive',
                'icon'  => 'tick'
            )
        ));
        return $this;
    }
    /**
     * Запускает форму на выполнение
     *
     * @return Core_Role_EditForm
     */    
    public function process()
    {
        $role = $this->getParams('role');
        $values = $this->getValues(true);
        $role->is_login = $values['is_login'];
        $role->save();
        $tableRolesTies = new Core_Table_RoleTies();
        $data = array('role_id' => $role->id);
        $tableRolesTies->delete(array('role_id = ?' => $role->id));
        if (is_array($values['parents'])) {
            foreach ($values['parents'] as $key => $value) {
                if ($value) {
                    $data['parent'] = $key;
                    $tableRolesTies->insert($data);
                }
            }
        }
        Core_Message::add('Данные сохранены', Core_Message::TYPE_SUCCESS);
        return $this;        
    }
}